package com.rocky.project.action.model;

import java.util.Iterator;
import java.util.LinkedList;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.opensymphony.webwork.ServletActionContext;
import com.rocky.project.action.base.BaseAction;
import com.rocky.project.model.admin.Resource;
import com.rocky.project.model.admin.Role;
import com.rocky.project.model.admin.RoleResourceR;
import com.rocky.project.util.WebUtil;

public class RoleAction extends BaseAction {
	private static final long serialVersionUID = -3510474656970657392L;

	private Role role;
	private Resource resource;

	private RoleResourceR  roleResourceR;	

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String createProcess() throws Exception {
		
		JSONObject obj = JSONObject.fromObject(this.jsondata);
		
		role = (Role) JSONObject.toBean(obj, Role.class);
		WebUtil.init4create(role);
		role.setStatus("active");

		baseService.create(role);
		obj = JSONObject.fromObject(role);
		this.ajaxResultMap.put("result", obj.toString());
		return ajaxResult;

	}

	@SuppressWarnings("unchecked")
	@Override
	public String read() throws Exception {

		java.util.List<Object> paramsvalues = new LinkedList<Object>();
		String queryHql = "SELECT m FROM Role AS m WHERE m.deleted = false and m.status = 'active'   ";

		queryHql += "  and  1=?      ";
		paramsvalues.add(1);

		queryHql += "  order by m.id desc   ";
		this.pageRecords = baseService.executeHqlWithParam(queryHql,paramsvalues.toArray(),0, null);
		
		if(this.from!=null && this.from.equals("combox")){
			this.ajaxResultMap.put("data", pageRecords);
			
//			this.jsonMarshaller.exclude("permissionSet");//关联属性不转化	
//			this.jsonMarshaller.exclude("userSet");//关联属性不转化			

			ServletActionContext.getResponse().setContentType("text/plain charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(JSONArray.fromObject(this.ajaxResultMap, jsonConfig).toString());	
			
			return ajaxResult;			
		}
		
		return "roletree";
	}

	@SuppressWarnings("unchecked")
	@Override
	public String updateProcess() throws Exception {
		
		JSONObject obj = JSONObject.fromObject(this.jsondata);
		obj.remove("endDate");
		role = (Role) JSONObject.toBean(obj, Role.class);
		
		Role roleDB = (Role) baseService.get(Role.class, role.getId());
		roleDB.setRolename(role.getRolename());
		roleDB.setRolecode(role.getRolecode());
		roleDB.setMemo(role.getMemo());

		WebUtil.init4modify(roleDB);
		baseService.merge(roleDB);
		return ajaxResult;
	}

	@Override
	public String deleteProcess() throws Exception {
		String[] ids = WebUtil.getParaValue("ids").split(",");
		for (String id : ids) {
			Role roleDB = (Role) baseService.get(Role.class, Long.valueOf(id));
			baseService.delete(roleDB);
		}
		return ajaxResult;
	}
	@SuppressWarnings("unchecked")
	public String readRoleResourceR() throws Exception {
		java.util.List<Object> paramsvalues = new LinkedList<Object>();
		
		String queryHql = "SELECT m.resource FROM RoleResourceR  AS m WHERE m.deleted = false   ";
		queryHql += "  and  m.role.id =?      ";
		paramsvalues.add(Long.valueOf(this.id));		
//		queryHql += "  and  m.resource.resourceType=?      ";
//		paramsvalues.add(this.from);
		queryHql += "  order by m.resource.showindex asc   ";
		this.pageRecords = baseService.executeHqlWithParam(queryHql,paramsvalues.toArray(),0, null);
		this.rowCount = baseService.getCountHqlWithParam(queryHql, paramsvalues.toArray());		
//		this.ajaxResultMap.put("rowCount", rowCount);
//		this.ajaxResultMap.put("result", pageRecords);
//		this.jsonMarshaller.exclude("parentResouce");//关联属性不转化	
//		this.jsonMarshaller.exclude("items");//关联属性不转化	
//		this.jsonMarshaller.exclude("permissionSet");//关联属性不转化	
		return "roleResourceR";
	}	
	
	@SuppressWarnings("unchecked")
	public String addRoleResourceR() throws Exception {			
		String roleId = WebUtil.getParaValue("roleId");		
		this.pageRecords = baseService.executeHqlNoParam("select r  from  RoleResourceR r where  r.role.id = '"+roleId+"'  and r.resource.resourceType='"+this.from+"'", 0, null);
		for(Iterator<RoleResourceR> iter = this.pageRecords.iterator();iter.hasNext();){
			roleResourceR =(RoleResourceR) iter.next();
			baseService.deleteTrue(roleResourceR);
		}
		role =(Role) baseService.get(Role.class, Long.valueOf(roleId));
		this.ids = this.id.split(",");
		for(String resourceId :this.ids){
			resource = null;
			resource =(Resource) baseService.get(Resource.class, Long.valueOf(resourceId));
			if(resource!=null){
				roleResourceR = new RoleResourceR();
				roleResourceR.setRole(role);
				roleResourceR.setResource(resource);
				baseService.create(roleResourceR);
			}
		}		
		return ajaxResult;
	}

	}		
	
	

	
	

